#! /bin/bash
MASTER_IP=192.168.1.88
NETWORK_SEGMENT=192.168.1
# 设置ip
echo "Environment=\"KUBELET_EXTRA_ARGS=--node-ip=$(ip a | grep -w 'inet' | grep 'global' | sed 's/^.*inet //g' | sed 's/\/[0-9][0-9].*$//g' | grep $NETWORK_SEGMENT)\"" | tee -a /etc/systemd/system/kubelet.service.d/10-kubeadm.conf
systemctl daemon-reload
systemctl restart kubelet
# 初始化集群
kubeadm reset -f
kubeadm init --apiserver-advertise-address=$MASTER_IP --apiserver-cert-extra-sans=$MASTER_IP --pod-network-cidr=172.172.0.0/16 --image-repository registry.aliyuncs.com/google_containers --kubernetes-version 1.25.4 --ignore-preflight-errors Swap
# 配置kubectl的config
mkdir -p $HOME/.kube
cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
chown $(id -u):$(id -g) $HOME/.kube/config
mkdir -p /home/vagrant/.kube
cp -i /etc/kubernetes/admin.conf /home/vagrant/.kube/config
chown vagrant:root /home/vagrant/.kube/config
# 导出join脚本
config_path="/vagrant/configs"
if [ -d $config_path ]; then
  rm -f $config_path/*
else
  mkdir -p /vagrant/configs
fi
touch /vagrant/configs/join.sh
chmod +x /vagrant/configs/join.sh
kubeadm token create --print-join-command >/vagrant/configs/join.sh
# 安装calico
kubectl apply -f https://docs.projectcalico.org/manifests/calico.yaml
# 安装nfs
apt install nfs-kernel-server -y
mkdir -p /data/nfs/
chmod a+rwx -R /data/nfs
chown nobody:nogroup /data/nfs/
sed -i "$ a /data/nfs *(async,insecure,no_root_squash,no_subtree_check,rw)" /etc/exports
exportfs -ra
systemctl restart nfs-kernel-server
# 安装nfs storageClass
kubectl apply -f /vagrant/nfs/
# 取消master污点(为了安装istio)
kubectl taint nodes master node-role.kubernetes.io/control-plane-
# 安装istio
tar -zxvf /vagrant/istio-1.16.0-linux-amd64.tar.gz -C /opt/
sed -i "$ a export PATH=/opt/istio-1.16.0/bin:\$PATH" .bashrc
export PATH=/opt/istio-1.16.0/bin:$PATH
istioctl install -y --set profile=default
# 安装helm
mkdir -p /opt/helm/bin
cp /vagrant/helm /opt/helm/bin/
sed -i "$ a export PATH=/opt/helm/bin/:\$PATH" .bashrc
# 安装kubectl 自动补全
apt install -y bash-completion
source /usr/share/bash-completion/bash_completion
source <(kubectl completion bash)
echo "source <(kubectl completion bash)" >>~/.bashrc
# 修改istio网关负载均衡ip为masterIP方便后续配置
kubectl patch services -n istio-system istio-ingressgateway --patch '{"spec": {"externalIPs": ["'$MASTER_IP'"]}}'
source .bashrc